﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using SF.DBUtility;
using SF.Common;
namespace SF.DAL
{
    //sf_auto_reply
    public partial class sf_auto_reply
    {

        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_auto_reply");
            strSql.Append(" where ");
            strSql.Append(" id = @id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(SF.Model.sf_auto_reply model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sf_auto_reply(");
            strSql.Append("busnieseNum,appNum,templatesNum,replyText,isDefault");
            strSql.Append(") values (");
            strSql.Append("@busnieseNum,@appNum,@templatesNum,@replyText,@isDefault");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@appNum", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@templatesNum", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@replyText", SqlDbType.NVarChar,-1) ,            
                        new SqlParameter("@isDefault", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.busnieseNum;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.templatesNum;
            parameters[3].Value = model.replyText;
            parameters[4].Value = model.isDefault;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                if (model.isDefault == 1)
                {
                    model.id = Convert.ToInt64(obj);
                    Update(model);
                }
                return Convert.ToInt64(obj);
            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SF.Model.sf_auto_reply model)
        {

            System.Collections.Generic.List<CommandInfo> sqllist = new System.Collections.Generic.List<CommandInfo>();
            CommandInfo cmd;
            if (model.isDefault == 1)
            {
                StringBuilder s1 = new StringBuilder();
                s1.Append("update sf_auto_reply set isDefault=0 where busnieseNum=@busnieseNum and appNum=@appNum and templatesNum=@templatesNum ");
                SqlParameter[] p1 = {
                                            new SqlParameter("@busnieseNum",SqlDbType.NVarChar,50),
                                            new SqlParameter("@appNum",SqlDbType.NVarChar,50),
                                            new SqlParameter("@templatesNum",SqlDbType.NVarChar,50)
                                        };
                p1[0].Value = model.busnieseNum;
                p1[1].Value = model.appNum;
                p1[2].Value = model.templatesNum;
                cmd = new CommandInfo(s1.ToString(), p1);
                sqllist.Add(cmd);
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sf_auto_reply set ");

            strSql.Append(" busnieseNum = @busnieseNum , ");
            strSql.Append(" appNum = @appNum , ");
            strSql.Append(" templatesNum = @templatesNum , ");
            strSql.Append(" replyText = @replyText , ");
            strSql.Append(" isDefault = @isDefault  ");
            strSql.Append(" where id=@id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@id", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@appNum", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@templatesNum", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@replyText", SqlDbType.NVarChar,-1) ,            
                        new SqlParameter("@isDefault", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.id;
            parameters[1].Value = model.busnieseNum;
            parameters[2].Value = model.appNum;
            parameters[3].Value = model.templatesNum;
            parameters[4].Value = model.replyText;
            parameters[5].Value = model.isDefault;

            cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            DbHelperSQL.ExecuteSqlTranWithIndentity(sqllist);
            return true;
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sf_auto_reply ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sf_auto_reply ");
            strSql.Append(" where ID in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_auto_reply GetModel(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, busnieseNum, appNum, templatesNum, replyText, isDefault  ");
            strSql.Append("  from sf_auto_reply ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;


            SF.Model.sf_auto_reply model = new SF.Model.sf_auto_reply();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = long.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.busnieseNum = ds.Tables[0].Rows[0]["busnieseNum"].ToString();
                model.appNum = ds.Tables[0].Rows[0]["appNum"].ToString();
                model.templatesNum = ds.Tables[0].Rows[0]["templatesNum"].ToString();
                model.replyText = ds.Tables[0].Rows[0]["replyText"].ToString();
                if (ds.Tables[0].Rows[0]["isDefault"].ToString() != "")
                {
                    model.isDefault = int.Parse(ds.Tables[0].Rows[0]["isDefault"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_auto_reply GetModel(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id, busnieseNum, appNum, templatesNum, replyText, isDefault  ");
            strSql.Append("  from sf_auto_reply ");
            if (where!="")
            {
                strSql.Append(" where "+where);
            }

            SF.Model.sf_auto_reply model = new SF.Model.sf_auto_reply();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = long.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.busnieseNum = ds.Tables[0].Rows[0]["busnieseNum"].ToString();
                model.appNum = ds.Tables[0].Rows[0]["appNum"].ToString();
                model.templatesNum = ds.Tables[0].Rows[0]["templatesNum"].ToString();
                model.replyText = ds.Tables[0].Rows[0]["replyText"].ToString();
                if (ds.Tables[0].Rows[0]["isDefault"].ToString() != "")
                {
                    model.isDefault = int.Parse(ds.Tables[0].Rows[0]["isDefault"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM sf_auto_reply ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM sf_auto_reply ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM sf_auto_reply");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(string strValue, string where)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update sf_auto_reply set " + strValue);
                strSql.Append(" where " + where);
                DbHelperSQL.ExecuteSql(strSql.ToString());
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }

    }
}

